from PyQt5.QtWidgets import (QWidget, QFormLayout, QSpinBox, QGroupBox, 
                          QVBoxLayout, QCheckBox, QHBoxLayout, QPushButton, 
                          QLabel, QLineEdit, QFileDialog)
import os

class SettingsTab(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.init_ui()
        
    def init_ui(self):
        layout = QFormLayout(self)
        
        # Registration quantity
        self.number_spin = QSpinBox()
        self.number_spin.setRange(1, 100)
        self.number_spin.setValue(5)
        layout.addRow("注册数量:", self.number_spin)
        
        # Max threads
        self.max_workers_spin = QSpinBox()
        self.max_workers_spin.setRange(1, 10)
        self.max_workers_spin.setValue(5)
        layout.addRow("最大线程数:", self.max_workers_spin)
        
        # Chrome browser settings
        browser_group = QGroupBox("Chrome浏览器设置")
        browser_layout = QFormLayout()
        
        # Chrome executable path
        chrome_path_layout = QHBoxLayout()
        self.chrome_path_edit = QLineEdit()
        self.chrome_path_edit.setPlaceholderText("自动检测")
        
        browse_button = QPushButton("浏览...")
        browse_button.clicked.connect(self.browse_chrome_path)
        
        chrome_path_layout.addWidget(self.chrome_path_edit)
        chrome_path_layout.addWidget(browse_button)
        browser_layout.addRow("Chrome路径:", chrome_path_layout)
        
        # User data directory
        user_data_layout = QHBoxLayout()
        self.user_data_edit = QLineEdit()
        self.user_data_edit.setPlaceholderText("默认")
        
        browse_user_data_button = QPushButton("浏览...")
        browse_user_data_button.clicked.connect(self.browse_user_data_path)
        
        user_data_layout.addWidget(self.user_data_edit)
        user_data_layout.addWidget(browse_user_data_button)
        browser_layout.addRow("用户数据目录:", user_data_layout)
        
        # Additional Chrome options
        self.disable_gpu_checkbox = QCheckBox("禁用GPU加速")
        browser_layout.addRow("", self.disable_gpu_checkbox)
        
        self.disable_extensions_checkbox = QCheckBox("禁用扩展")
        browser_layout.addRow("", self.disable_extensions_checkbox)
        
        self.incognito_checkbox = QCheckBox("无痕模式")
        browser_layout.addRow("", self.incognito_checkbox)
        
        browser_group.setLayout(browser_layout)
        layout.addRow(browser_group)
        
        # Options
        options_group = QGroupBox("选项设置")
        options_layout = QVBoxLayout()
        
        # 修改无头模式默认为关闭，并添加注释
        headless_layout = QHBoxLayout()
        self.headless_checkbox = QCheckBox("无头模式")
        self.headless_checkbox.setChecked(False)  # 默认关闭无头模式
        self.headless_checkbox.setToolTip("启用无头模式，浏览器将在后台运行，不显示界面")
        headless_layout.addWidget(self.headless_checkbox)
        
        headless_warning = QLabel("（无头模式有概率注册失败）")
        headless_warning.setStyleSheet("color: #FF5555;")  # 红色警告文字
        headless_layout.addWidget(headless_warning)
        headless_layout.addStretch()
        options_layout.addLayout(headless_layout)
        
        self.browser_log_checkbox = QCheckBox("启用浏览器日志")
        self.browser_log_checkbox.setChecked(True)
        options_layout.addWidget(self.browser_log_checkbox)
        
        self.hide_info_checkbox = QCheckBox("隐藏账号信息")
        options_layout.addWidget(self.hide_info_checkbox)
        
        self.auto_close_browser_checkbox = QCheckBox("自动关闭浏览器")
        self.auto_close_browser_checkbox.setChecked(True)  # 默认自动关闭浏览器
        self.auto_close_browser_checkbox.setToolTip("注册完成后自动关闭浏览器窗口")
        options_layout.addWidget(self.auto_close_browser_checkbox)
        
        options_group.setLayout(options_layout)
        layout.addRow(options_group)
        
        # Try to detect Chrome path automatically
        self.detect_chrome_path()
    
    def detect_chrome_path(self):
        """自动检测Chrome浏览器路径"""
        possible_paths = [
            # Windows paths
            r"C:\Program Files\Google\Chrome\Application\chrome.exe",
            r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe",
            # Add more common paths if needed
        ]
        
        for path in possible_paths:
            if os.path.exists(path):
                self.chrome_path_edit.setText(path)
                break
    
    def browse_chrome_path(self):
        """浏览选择Chrome可执行文件"""
        file_path, _ = QFileDialog.getOpenFileName(
            self, "选择Chrome可执行文件", "", "可执行文件 (*.exe)"
        )
        
        if file_path:
            self.chrome_path_edit.setText(file_path)
    
    def browse_user_data_path(self):
        """浏览选择用户数据目录"""
        dir_path = QFileDialog.getExistingDirectory(
            self, "选择用户数据目录"
        )
        
        if dir_path:
            self.user_data_edit.setText(dir_path)
    
    def get_settings(self):
        """
        Get current settings as a dictionary
        """
        return {
            "number": self.number_spin.value(),
            "max_workers": self.max_workers_spin.value(),
            "enable_headless": self.headless_checkbox.isChecked(),
            "enable_browser_log": self.browser_log_checkbox.isChecked(),
            "hide_account_info": self.hide_info_checkbox.isChecked(),
            "auto_close_browser": self.auto_close_browser_checkbox.isChecked(),  # 新增自动关闭浏览器选项
            "chrome_settings": {
                "chrome_path": self.chrome_path_edit.text(),
                "user_data_dir": self.user_data_edit.text(),
                "disable_gpu": self.disable_gpu_checkbox.isChecked(),
                "disable_extensions": self.disable_extensions_checkbox.isChecked(),
                "incognito": self.incognito_checkbox.isChecked()
            }
        } 